<?php

namespace App\Http\Requests;

use App\Project\Traits\Validator\GreaterThanBirthday;
use Illuminate\Database\Query\Builder;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
use Illuminate\Validation\Validator;

class PersonalHistoryStoreRequest extends FormRequest
{
    use GreaterThanBirthday;

    /**
     * Determine if the user is authorized to make this request.
     * @return bool
     */
    public function authorize()
    {
        return \Auth::check();
    }

    public function withValidator(Validator $validator)
    {
        $validator->after(function (Validator $validator) {
            if ($validator->messages()->isEmpty()) {
                $this->ifGreaterThanBirthday(
                    $validator,
                    ['smoke.time', 'drink.time'],
                    [
                        'smoke.time' => '吸烟时间 不能早于出生日期。',
                        'drink.time' => '戒烟时间 不能早于出生日期。',
                    ]);
            }
        });
    }

    /**
     * Get the validation rules that apply to the request.
     * @return array
     */
    public function rules()
    {
        return [
            'record_id' => [
                'required', 'integer', 'min:1',
                Rule::exists('st_medical_records', 'id')->where(function (Builder $query) {
                    $query->whereNull('deleted_at');
                    $user = \Auth::user();
                    if (!$user->isDoctorsAdmin() && !$user->isAdmin() && !$user->isPatient()) {
                        $query->where(['doctor_id' => $user->id]);
                    }
                }),
            ],
            'smoke.time' => 'nullable|date|before_or_equal:now',
            'smoke.many' => 'nullable|integer|min:1|max:999',
            'stop_smoke' => 'nullable|date|after_or_equal:smoke.time|before_or_equal:now',
            'drink.time' => 'nullable|date|before_or_equal:now',
            'drink.frequency' => 'in:1,2',
            'drink.whiteSpirit' => 'nullable|numeric|min:0|max:100000',
            'drink.redSpirit' => 'nullable|numeric|min:0|max:100000',
            'drink.beer' => 'nullable|numeric|min:0|max:100000',
            'stop_drink' => 'nullable|date|after_or_equal:drink.time|before_or_equal:now',
            'remark' => 'nullable|string|max:255',
            "drink.whiteSpiritType" => 'in:0,1,2',
            "drink.redSpiritType"   => 'in:0,1,2',
            "drink.beerSpiritType"   => 'in:0,1,2',
        ];
    }

    public function messages()
    {
        return [
            'record_id.required' => '缺少病历ID',
            'record_id.integer' => '病历ID 必须是整数。',
            'record_id.min' => '病历ID 必须大于等于 :min。',
            'record_id.exists' => '病历 不存在或无修改该病历的权限。',
            'smoke.time.date' => '吸烟时间 格式错误。',
            'smoke.time.before_or_equal' => '吸烟时间 必须要早于当前时间。',
            'smoke.many.required_with' => '当 吸烟时间 存在时 每日吸烟数量 不能为空。',
            'smoke.many.integer' => '每日吸烟数量必须是整数。',
            'smoke.many.min' => '每日吸烟数量必须大于等于 :min。',
            'stop_smoke.date' => '戒烟时间 格式错误。',
            'stop_smoke.after_or_equal' => '戒烟时间 必须要晚于 开始吸烟时间。',
            'stop_smoke.before_or_equal' => '戒烟时间 必须要早于当前时间。',
            'drink.time.date' => '饮酒时间 格式错误。',
            'drink.time.before_or_equal' => '饮酒时间 必须要早于当前时间。',
            'drink.frequency.required_with' => '当 饮酒时间 存在时 饮酒频率 不能为空。',
            'drink.frequency.in' => '饮酒频率 值错误。',
            'drink.whiteSpirit.numeric' => '白酒 饮用量必须是一个数字。',
            'drink.whiteSpirit.min' => '白酒 饮用量必须大于等于 :min。',
            'drink.redSpirit.numeric' => '红酒 饮用量必须是一个数字。',
            'drink.redSpirit.min' => '红酒 饮用量必须大于等于 :min。',
            'drink.beer.numeric' => '啤酒 饮用量必须是一个数字。',
            'drink.beer.min' => '啤酒 饮用量必须大于等于 :min。',
            'stop_drink.date' => '戒酒时间 格式错误。',
            'stop_drink.after_or_equal' => '戒酒时间 必须要晚于 开始饮酒时间。',
            'stop_drink.before_or_equal' => '戒酒时间 必须要早于当前时间。',
            'remark.max' => '备注最大 :max 个字符',
        ];
    }
}
